/* eslint-disable no-cond-assign */
/* eslint-disable react/sort-comp */
import React, { Component } from "react";
import { Button, Form, Input, Checkbox, Switch, Select, message, InputNumber } from "antd";
import { messages } from "share/common";
import { connect } from "react-redux";
import "styles/supplier-management/new-consumer-management/edit-frams/edit-frame.scss";
import ReasonsServer from "./reasons-server";

const FormItem = Form.Item;
const Group = Checkbox.Group;
const { Option } = Select;

@Form.create()
class ReasonsConfigEdit extends Component {
  constructor(props) {
    super(props);
    this.state = {
      editData: props.editData,
      isNew: props.isAdd,
    };
  }

  onSubmitEdit = () => {
    const { isNew, editData } = this.state;
    const { companyOID, id, source } = this.props;
    this.props.form.validateFieldsAndScroll((err, values) => {
      if (!err) {
        const params = Object.assign({}, editData, values, {
          companyOid: companyOID,
          type: getReasonType(id),
        });
        const type = isNew ? "addData" : "alterData";
        this.props.close({ type, data: params });
        // if (source === "airplane") {
        //   ReasonsServer(this)
        //     .saveAirplaneReasons(params)
        //     .then((res) => {
        //       if (res && res.data && res.data.code === 200) {
        //         message.success(res.data.message);
        //         this.props.close({ type, data: params });
        //       } else {
        //         message.error(res.data.message);
        //       }
        //     });
        // } else if (source === "hotel") {
        //   ReasonsServer(this)
        //     .saveHotelReasons(params)
        //     .then((res) => {
        //       if (res && res.data && res.data.code === 200) {
        //         message.success(res.data.message);
        //         this.props.close({ type, data: params });
        //       } else {
        //         message.error(res.data.message);
        //       }
        //     });
        // }
      }
    });
  };

  onCancelEdit = () => {
    this.props.close({ type: "onClose" });
  };

  render() {
    const { getFieldDecorator, getFieldValue } = this.props.form;
    const { editData } = this.state;

    const formLayout = {
      labelCol: { span: 6, offset: 1 },
      wrapperCol: { span: 14 },
    };

    return (
      <div className="edit-frame">
        <Form className="new-form">
          <FormItem label={messages("common.column.status") /*状态*/} {...formLayout}>
            {getFieldDecorator("isEnable", {
              initialValue: editData.isEnable || false,
              valuePropName: "checked",
            })(<Switch />)}
          </FormItem>

          <FormItem label={messages("supplier-1.key856") /*值名称*/} {...formLayout}>
            {getFieldDecorator("reasonName", {
              initialValue: editData.reasonName || "",
              rules: [
                {
                  required: true,
                  message: messages("supplier-1.key193") /*必填*/,
                },
              ],
            })(<Input />)}
          </FormItem>
          <FormItem label={messages("supplier-1.key855") /*编码*/} {...formLayout}>
            {getFieldDecorator("reasonCode", {
              initialValue: editData.reasonCode || "",
              rules: [
                {
                  required: true,
                  message: messages("supplier-1.key193") /*必填*/,
                },
              ],
            })(<Input />)}
          </FormItem>
          <FormItem label={messages("supplier-1.key854") /*顺序号*/} {...formLayout}>
            {getFieldDecorator("sortNumber", {
              initialValue: editData.sortNumber || "",
              rules: [
                {
                  required: true,
                  message: messages("supplier-1.key193") /*必填*/,
                },
              ],
            })(<InputNumber />)}
          </FormItem>
        </Form>
        <br />

        <div className="affix-bottom">
          <Button type="primary" onClick={this.onSubmitEdit}>
            {messages("common.ok") /*确定*/}
          </Button>
          &nbsp;&nbsp;&nbsp;&nbsp;
          <Button onClick={this.onCancelEdit}>{messages("common.cancel") /*取消*/}</Button>
        </div>
      </div>
    );
  }
}

const getReasonType = (id) => {
  const resonTypeMap = {
    reasonForNotLowestPriceList: 1,
    reasonForBeyondStandardsList: 2,
    voluntaryChangeReasonList: 3,
    involuntaryChangeReasonList: 4,
    voluntaryRefundReasonList: 5,
    involuntaryRefundReasonList: 6,
    reasonForNotAgreementList: 1,
  };
  return resonTypeMap[id];
};

function mapStateToProps(state) {
  return {
    tenant: state.login.tenant,
  };
}

export default connect(mapStateToProps)(ReasonsConfigEdit);
